<template>
  <Container class="more-sale-rules">
    <div slot="header" class="container-header title title-center">
      多买优惠促销
      <Button size="large" class="fl btn-back" icon="ios-arrow-back" @click="jumpPrevPage">返回</Button>
    </div>
    <div class="main">
      <DefaultEditItem ref="moreSaleForm" :form="$store.state.moreSale.MoresaleForm" :rules="rules" />
    </div>
    <div slot="footer" class="container-footer bg-white">
      <Button type="primary" size="large" class="ml" @click="onShowList">下一步</Button>
    </div>
  </Container>
</template>
<script>
import DefaultEditItem from './edit/DefaultEditItem'
export default {
  name: 'MoreSaleRules',
  components: {
    DefaultEditItem
  },
  data () {
    let validatemain = function (rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入商品数量'))
      } else if (!/^([2-9]|(1[0-9]|2[0]))$/.test(value)) {
        return callback(new Error('数量必须在2-20件'))
      } else {
        callback()
      }
    }
    const validateMoreMoney = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入价格'))
      } else {
        callback()
      }
    }
    const validatePiece = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入件数'))
      } else if (!(/^[1-8]$/).test(value)) {
        return callback(new Error('件数在1-8之间'))
      } else {
        callback()
      }
    }
    return {
      id: this.$route.query.id || '',
      type: this.$route.query.type || '',
      isEmpty: false, // 是否清空数据
      isSave: true, // 是否保存数据
      rules: {
        name: [{ required: true, message: '请输入促销名称', trigger: 'blur' }],
        date: [
          {
            required: true,
            message: '请开始时间',
            trigger: 'change',
            type: 'array'
            // fields: {
            //   0: { type: "date", required: true, message: "请输入起止日期" },
            //   1: { type: "date", required: true, message: "请输入起止日期" }
            // }
          }
        ],
        grade: [
          {
            required: true,
            message: '请选择活动对象',
            type: 'array',
            trigger: 'change'
          }
        ],
        gender: [
          { required: true, message: '请选择参与方式', trigger: 'change' }
        ],
        main: [{ validator: validatemain, trigger: 'blur' }],
        second: [{ validator: this.validatesecond, trigger: 'blur' }],
        moreMoney: [{ validator: validateMoreMoney, trigger: 'blur' }],
        piece: [{ validator: validatePiece, trigger: 'blur' }]
      }
    }
  },
  mounted () {
    // this.id && this.getDetailData()
  },
  methods: {
    getDetailData () {
      this.$ajaxGet('/activity/buy_more/detail/' + `${this.id}`).then(data => {
        this.$store.state.moreSale.MoresaleForm.remarks = data.activityInfoDTO.activityDescription//活动备注
        this.$store.state.moreSale.MoresaleForm.name = data.activityInfoDTO.activityName //促销名称
        this.$store.state.moreSale.MoresaleForm.grade = this.promotionMemberScope = data.activityInfoDTO.storeTypeEnumList //活动对象
        this.$store.state.moreSale.MoresaleForm.date = [new Date(data.activityInfoDTO.startDate), new Date(data.activityInfoDTO.endDate)]
        let gender = ''
        if (data.activityInfoDTO.joinTypeEnum == 'WHOLE') {
          gender = '1'
        } else if (data.activityInfoDTO.joinTypeEnum == 'PART') {
          gender = '2'
        } else {
          gender = '3'
        }
        this.$store.state.moreSale.MoresaleForm.gender = gender
        if (data.activityBuyMoreDTO.buyMoreTypeEnum == 'M_MINUS_N') { //满M件减N件
          this.$store.state.moreSale.MoresaleForm.type = '1'
        } else if (data.activityBuyMoreDTO.buyMoreTypeEnum == 'M_DISCOUNT_N') { // M件N折
          this.$store.state.moreSale.MoresaleForm.type = '2'
        } else if (data.activityBuyMoreDTO.buyMoreTypeEnum == 'M_SELECT_N') { //M元任选N件
          this.$store.state.moreSale.MoresaleForm.type = '3'
        }
        if (data.activityBuyMoreDTO.buyMoreTypeEnum == 'M_DISCOUNT_N') { // M件N折
          this.$store.state.moreSale.MoresaleForm.type2 = data.activityBuyMoreDTO.buyMoreRuleList.map(item => {
            return {
              value: item.m,
              n: item.n,
              isParcel: item.isParcel
            }
          })
          this.$store.state.moreSale.MoresaleForm.type1 = data.activityBuyMoreDTO.buyMoreRuleList.map(item => {
            return {
              value: item.n,
              n: item.n
            }
          })
        } else if (data.activityBuyMoreDTO.buyMoreTypeEnum == 'M_MINUS_N') { // M件N件
          this.$store.state.moreSale.MoresaleForm.main = data.activityBuyMoreDTO.buyMoreRuleList[0].m
          this.$store.state.moreSale.MoresaleForm.second = data.activityBuyMoreDTO.buyMoreRuleList[0].n
          this.$store.state.moreSale.MoresaleForm.isParcel = data.activityBuyMoreDTO.buyMoreRuleList[0].isParcel
        } else if (data.activityBuyMoreDTO.buyMoreTypeEnum == 'M_SELECT_N') {
          this.$store.state.moreSale.MoresaleForm.moreMoney = data.activityBuyMoreDTO.buyMoreRuleList[0].m
          this.$store.state.moreSale.MoresaleForm.piece = data.activityBuyMoreDTO.buyMoreRuleList[0].n
          this.$store.state.moreSale.MoresaleForm.isParcel = data.activityBuyMoreDTO.buyMoreRuleList[0].isParcel
        }
      })
    },
    jumpPrevPage () {
      this.$router.back()
    },
    onShowList () {
      let self = this
      this.$refs['moreSaleForm'].$refs['form'].validate(valid => {
        if (valid) {
          this.$Message.success('保存成功!')
          let url = { name: 'sales/more/list', query: {} }
          if (self.id) {
            url.query.id = self.id
            if (self.type) url.query.type = self.type
          }
          this.$router.push(url)
        }
      })
    },
    validatesecond (rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入商品数量'))
      } else if (!/^([1-7])$/.test(value)) {
        return callback(new Error('数量必须在1-7件'))
      } else if (this.$store.state.moreSale.MoresaleForm.main <= this.$store.state.moreSale.MoresaleForm.second) {
        return callback(new Error('减件数必须小于满件数'))
      } else {
        callback()
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    /*
        1. 路由从不是下一页进入的都清空
        2. 路由从下一页进入并且有id就不刷新接口--保存信息
      */
    next(vm => {
      if (from.name !== 'sales/more/list') {
        vm.isEmpty = true
      } else {
        vm.isSave = false
      }
      if (vm.isEmpty) vm.$store.commit('setMoreSaleEmpty')
      if (vm.id && vm.isSave) vm.getDetailData()
    })
  }
}
</script>
